<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户名验证</title>
</head>
<style>
    span {
        font-size: 12px;
    }

    .right {
        color: green;
    }

    .wrong {
        color: red;
    }
</style>

<body>
    <!-- 功能需求：1.用户输入合法，后面提示信息为:用户名合法，并且颜色为绿色
    2. 用户输入不合法，后面提示信息为：用户名不符合规范，颜色为红色 -->
    <input type="text" class="uname">
    <span>请输入用户名</span>

    <script>
        var reg = /^[a-zA-Z0-9]{6,11}$/
        var uname = document.querySelector('.uname')
        var span = document.querySelector('span')
        uname.onblur = function () {
            if (reg.test(this.value)) {
                console.log('true');
                span.className = 'right'
                span.innerHTML = '用户名输入正确'
            } else {
                console.log('false');
                span.className = 'wrong'
                span.innerHTML = '用户名格式错误'
            }
        }
    </script>
</body>

</html>